package com.ysd.modules.app.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.modules.app.dao.ChaptersDao;
import com.ysd.modules.app.dao.CollectDao;
import com.ysd.modules.app.dao.CollectTopicDao;
import com.ysd.modules.app.dao.OptionsDao;
import com.ysd.modules.app.dao.TopicDao;
import com.ysd.modules.app.entity.ChapterEntity;
import com.ysd.modules.app.entity.CollectEntity;
import com.ysd.modules.app.entity.CollectTopic;
import com.ysd.modules.app.entity.OptionsEntity;
import com.ysd.modules.app.entity.TopicEntity;
import com.ysd.modules.app.service.CollectService;
import com.ysd.modules.app.service.TopicsService;
import com.ysd.modules.sys.dao.ChapterDao;
import com.ysd.modules.sys.dao.TopicSysDao;
import com.ysd.modules.sys.entity.Chapter;

@Service
public class CollectServiceImpl extends ServiceImpl<CollectDao, CollectEntity> implements CollectService {

	@Autowired
	private CollectTopicDao collectTopicDao;
	
	@Autowired
	private TopicDao topicDao;
	
	@Autowired
	private ChaptersDao chaptersDao;
	
	@Autowired
	private OptionsDao optionsDao;
	
	@Override
	public List<TopicEntity> getCollectByStuId(Integer stuId, Integer chapterId) {
		ChapterEntity chapter = chaptersDao.selectOne(new QueryWrapper<ChapterEntity>().eq("id", chapterId));
		QueryWrapper<CollectEntity> wapper = new QueryWrapper<CollectEntity>();
		wapper.eq("student_id", stuId);
		if(chapterId != null) {
			wapper.eq("course_name", chapter.getCourseName());
			wapper.eq("chapter_name", chapter.getName());
		}
		List<CollectEntity> collectList = baseMapper.selectList(wapper);
		List<TopicEntity> tList = null;
		for(CollectEntity i:collectList) {
			tList = new ArrayList<TopicEntity>();
			Integer id = i.getId();
			List<CollectTopic> ctLsit = collectTopicDao.selectList(new QueryWrapper<CollectTopic>().eq("collect_id", id));
			for(CollectTopic c: ctLsit) {
				Integer topicId = c.getTopicId();
				TopicEntity topic = topicDao.selectById(topicId);
				List<OptionsEntity> optionList = optionsDao.selectList(new QueryWrapper<OptionsEntity>().eq("topic_id", topic.getId()));
				topic.setOptionsList(optionList);
				tList.add(topic);
			}
			i.setTList(tList);
		}
		return tList;
	}

	@Override
	public int addCollect(Integer chapterId, Integer topicId, Integer stuId) {
		ChapterEntity chapter = chaptersDao.selectOne(new QueryWrapper<ChapterEntity>().eq("id", chapterId));
		QueryWrapper<CollectEntity> wapper = new QueryWrapper<CollectEntity>();
		wapper.eq("course_name", chapter.getCourseName());
		wapper.eq("chapter_name", chapter.getName());
		CollectEntity selectOne = baseMapper.selectOne(wapper);
		if(selectOne == null) {
			CollectEntity c = new CollectEntity();
			c.setStudentId(stuId);
			c.setCourseName(chapter.getCourseName());
			c.setChapterName(chapter.getName());
			int i = baseMapper.insert(c);
			if(i > 0) {
				CollectTopic ct = new CollectTopic();
				ct.setCollectId(c.getId());
				ct.setTopicId(topicId);
				return collectTopicDao.insert(ct);
			}else {
				return 0;
			}
		}else {
			CollectTopic ct = new CollectTopic();
			ct.setCollectId(selectOne.getId());
			ct.setTopicId(topicId);
			return collectTopicDao.insert(ct);
		}
	}

	
	@Override
	public int delCollect(String courseName, String chapterName, Integer topicId, Integer stuId) {
		QueryWrapper<CollectEntity> wapper = new QueryWrapper<CollectEntity>();
		wapper.eq("course_name", courseName);
		wapper.eq("chapter_name", chapterName);
		wapper.eq("student_id", stuId);
		CollectEntity one = baseMapper.selectOne(wapper);
		List<CollectTopic> selectList = collectTopicDao.selectList(new QueryWrapper<CollectTopic>().eq("collect_id", one.getId()));
		
		if(selectList.size() != 1 && selectList.size() > 0) {
			return collectTopicDao.delete(new QueryWrapper<CollectTopic>().eq("topic_id", topicId));
		}else {
			int i1 = collectTopicDao.delete(new QueryWrapper<CollectTopic>().eq("topic_id", topicId));
			int i2 = baseMapper.delete(new QueryWrapper<CollectEntity>().eq("id", one.getId()));
			if(i1 >0 && i2 > 0) {
				return 1;
			}else {
				return 0;
			}
		}
	}

	@Override
	public List<Map<String, Object>> getCollectNumber(Integer stuId) {
		
		List<Map<String, Object>> cellectList = new ArrayList<Map<String,Object>>();
		List<CollectEntity> selectList = baseMapper.selectList(new QueryWrapper<CollectEntity>().eq("student_id", stuId));
		for(CollectEntity c : selectList) {
			Integer count = collectTopicDao.selectCount(new QueryWrapper<CollectTopic>().eq("collect_id", c.getId()));
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("cousreName", c.getCourseName());
			map.put("chapterId", c.getChapterId());
			map.put("chapterName", c.getChapterName());
			map.put("count", count);
			cellectList.add(map);
		}
		return cellectList;
	}

	

}
